package com.ziyun.pop.mall.module.goods.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.service.cart.api.CartService;
import com.service.cart.model.CartResult;
import com.service.cart.model.cart.AddCartDto;
import com.service.common.constant.AppType;
import com.service.goods.api.GoodsService;
import com.service.goods.constant.GoodsResult;
import com.service.quote.module.quote.model.QuoteDto;
import com.service.quote.module.quote.model.QuoteResult;
import com.utility.JsonUtils;
import com.ziyun.pop.mall.system.config.HttpRequestLocal;

import io.swagger.annotations.Api;

/**
 * @Author:chenxiaoyi
 * @Date:2018/9/29 16:05
 */
@Api(value ="POP报价商品Controller")
@Controller
public class PopQuoteGoodsController {

    private static Logger _log = LoggerFactory.getLogger(PopQuoteGoodsController.class);
    private static final String MODEL = "/quote";
    
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CartService cartService;

    @Autowired
    private HttpRequestLocal httpRequestLocal;

    /**
     * session保存当前算价信息
     * @param params 算价参数
     */
    @PostMapping(MODEL+"/quotePrice.do")
    @ResponseBody
    public GoodsResult<QuoteResult> quoteFindFactory(String params){
        QuoteDto quoteDto= JsonUtils.string2Object(params, QuoteDto.class);
        GoodsResult<QuoteResult>  factoryQuote = goodsService.findFactoryQuote(quoteDto);
         httpRequestLocal.setSessionValue("quoteDto", JsonUtils.object2String(quoteDto));
        return factoryQuote;
    }

    /**
     * 成功后跳转工厂列表
     */
    @GetMapping(MODEL+"/quoteProvider.html")
    public ModelAndView quoteProvider(){
        return new ModelAndView("/mall/provider/quoteProvider.html");
    }

    /**
     * 工厂列表后台数据
     */
    @PostMapping(MODEL+"/findFactory.json")
    @ResponseBody
    public  GoodsResult<QuoteResult> findFactory(){
        String dto = (String)httpRequestLocal.getSessionValue("quoteDto");
        QuoteDto quoteDto = JsonUtils.string2Object(dto, QuoteDto.class);
        return goodsService.findFactoryQuote(quoteDto);
    }


    /**
     * 报价商品加购物车
     */
    @PostMapping(MODEL+"/quoteAddCart.do")
    @ResponseBody
    public  CartResult<?> quoteAddCart(String addCartDto) {
        AddCartDto cartDto = JsonUtils.string2Object(addCartDto, AddCartDto.class);
        String dto = (String)httpRequestLocal.getSessionValue("quoteDto");
        QuoteDto quoteDto = JsonUtils.string2Object(dto, QuoteDto.class);
        cartDto.setQuoteSelfDefineParamMap(quoteDto.getDefinitionValueMap());
        cartDto.setQuoteAttrOptionMap(quoteDto.getAttributesMap());
        cartDto.setSelfPaperParts(quoteDto.getSelfPaperParts());
        return cartService.addItem(AppType.pc,null,cartDto);
    }

}

